// pages/course/index.js
var app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hidden:false,
    curNav: 1,
    curIndex: 0,
    cart: [],
    cartTotal: 0,
    dishes: [],
    navList: [{
      id: 1,
      name: '课程资料'
    },
    {
      id: 2,
      name: '法律法规'
    }],
    dishesList: [],
    server: app.data.server,
    height:0,
  },
  inputData:function(e){
    var inputvalue=e.detail.value;
    this.setData({
      searchVal: inputvalue
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.getSystemInfo({
      success: (res) => {
        this.setData({
          height: res.windowHeight
        })
      }
    })
    
    this.loadingChange();
    var dishesList = this.data.dishesList;
    var _this = this;
    app.paiRequest("/app/common/material/findPage", { parentCode: 0, objType:1, pageNo: 1, pageSize:10, item: 1 }).then(function (res) {
      console.log(res);
      var page=res.data;
      dishesList[0]=page;
      _this.setData({
        dishesList: dishesList
      });
    })    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  loadingChange:function(){
    setTimeout(() => {
      this.setData({
        hidden:true
      })
    },2000)
  },
  tapSearch:function(){
    var searchVal = this.data.searchVal;
    var objType = this.data.curNav;
    var curIndex = this.data.curIndex;
    var _this = this;
    var dishesList = this.data.dishesList;
    app.paiRequest("/app/common/material/findPage", {
      parentCode: 0,
      objType: objType,
      pageNo: 1,
      pageSize: 10,
      item: 1,
      objName: searchVal
    }).then(function (res) {
      console.log(res);
      var page = res.data;
      dishesList[curIndex] = page;
      _this.setData({
        dishesList: dishesList
      });
    })
  },
  selectNav:function(event){
       let id=event.target.dataset.id,index=parseInt(event.target.dataset.index);
       self=this;
       this.setData({
         curNav:id,
         curIndex:index
       })
    var dishesList = this.data.dishesList;
    var _this = this;
    app.paiRequest("/app/common/material/findPage", { 
      parentCode: 0, 
      objType: id, 
      pageNo: 1, 
      pageSize: 10, 
      item: 1 
    }).then(function (res) {
      console.log(res);
      var page = res.data;
      dishesList[index] = page;
      _this.setData({
        dishesList: dishesList
      });
    })
  },
  setStatus(dishId) {
    let dishes = this.data.dishesList;
    for (let dish of dishes) {
      dish.forEach((item) => {
        if (item.id == dishId) {
          item.status = !item.status || false
        }
      })
    }
    this.setData({
      dishesList: this.data.dishesList
    })
  },
  onScrollLower:function(){
    var dishesList = this.data.dishesList;
    var curIndex = this.data.curIndex;
    var curNav = this.data.curNav;
    var pageData=dishesList[curIndex];
    var _this = this;
    var count = pageData.count;
    var lent=pageData.list.length;
    if (count == lent) {
      wx.showToast({
        title: '到底了',
        icon: 'success',
        duration: 1000
      })
    } else {
      app.paiRequest("/app/common/material/findPage", { 
        parentCode: 0, 
        objType: curNav, 
        pageNo: pageData.pageNo+1, 
        pageSize: pageData.pageSize,
        item: 1 
      }).then(function (res) {
        console.log(res);
        var page = res.data;
        var list=pageData.list;
        var cont=list.concat(page.list);
        pageData.list = cont;
        pageData.pageNo=page.pageNo;
        dishesList[curIndex] = pageData;
        _this.setData({
          dishesList: dishesList
        });
      })
    }
  },
  selectDish:function(e){
    console.log(e);
    var src=e.currentTarget.dataset.src;
    var parentCode=e.currentTarget.dataset.dish;
    var navList = this.data.navList;
    var curIndex = this.data.curIndex;
    wx.navigateTo({
      url: src + "?parentCode=" + parentCode + "&title=" + navList[curIndex].name,
    })
  }
})